package jdbc;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class JdbcDmeo11
{
    // 关联查询
    public static void main(String[] args)
    {
        try(Connection connection = DBUtil.getConnection())
        {
            String sql = "select t.name Tname,t.salary ss,t.gender gen,t.title ti,c.name cn,c.floor cf\n" +
                    "from teacher t \n" +
                    "join class c on t.id = c.teacher_id\n" +
                    "where t.name = ?;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"范传奇");
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next())
            {
                // 可以用序列号(从1开始)     可以用字段名(没有别名用字段名，有别名用别名)
                String tname = resultSet.getString("Tname");
                int salary = resultSet.getInt(2);
                String gender = resultSet.getString(3);
                String title = resultSet.getString(4);
                String cn = resultSet.getString(5);
                int cf = resultSet.getInt(6);
                System.out.println(tname + "," + salary + "," + gender + "," + title + "," + cn + "," + cf  );
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}
